﻿<cfsilent>
	<cfscript>

		sessionAdvice = getProperty("serviceFactory").getBean("sessionAdvice");
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		response = structNew();
		
		structInsert(response, "success", false, true);

		teacherId = sessionAdvice.getAutherUserID();
		hashCode = event.getArg("Sign");
		taskId = event.getArg("TSK");
		
		try {
			
			if ( decrypt(hashCode, teacherId, "PBEWithMD5AndDES", "Hex") eq taskId ) {

				/* 载入期末成绩提交任务 */
				sql = "	SELECT
							ttm.task_id,
							ttm.task_prog
						FROM
							t_task_mark ttm
						WHERE
							ttm.task_id = :taskId ";
							
				queryObj = new Query(datasource = application.dnsSlave);
				
				queryObj.addParam(name="taskId", value=taskId, cfsqltype="cf_sql_varchar");
				
				rs_task = queryObj.execute(sql=sql).getResult();
				
				
				if ( rs_task.task_prog eq "0" or rs_task.task_prog eq "1" or rs_task.task_prog eq "2" or rs_task.task_prog eq "4" ) {
				
					transaction {

						/* 第三步 更新成绩提交数量 */
						sql = "SELECT 
								  count(tmm.scc_id)  mark_need,
								  SUM(
									  CASE
										  WHEN tmm.mark_note = '1' and tmm.mark_note_u = '1' and tmm.mark_note_m = '1' and tmm.mark_note_f = '1' and tmm.prop_id = '1' and (tmm.mark_u > 0 OR tmm.mark_u = 0) and (tmm.mark_m > 0 OR tmm.mark_m = 0) and (tmm.mark_f > 0 OR tmm.mark_f = 0) then
											  1
										  WHEN tmm.mark_note = '1' and tmm.mark_note_u = '1' and tmm.mark_note_m = '1' and tmm.mark_note_f = '1' and tmm.prop_id = '2' and (tmm.mark_u > 0 OR tmm.mark_u = 0) and (tmm.mark_f > 0 OR tmm.mark_f = 0) then
											  1
										  WHEN tmm.mark_mode = '4' AND (tmm.mark_f > 0 or tmm.mark_f = '0') then
											  1 
										  WHEN tmm.mark_mode = '2' AND (tmm.mark_f > 0 or tmm.mark_f = '0') then
											  1 
										  WHEN tmm.mark_note > '1' OR tmm.mark_note < '1' then
											  1
										  ELSE
											  0
										  END
									  )  mark_done
								  FROM t_temp_mark tmm 
								  WHERE tmm.task_id = :taskId  ";
						
						queryObj = new Query(datasource = application.dnsMaster);
						
						queryObj.addParam(name="taskId", value=taskId, cfsqltype="cf_sql_varchar");
						
						rs_mark_prog = queryObj.execute(sql=sql).getResult();

						sql = "	UPDATE
									t_task_mark ttm
								SET
									ttm.mark_done = :markDone ,
									ttm.mark_need = :markNeed 
								WHERE
									ttm.task_id = :taskId ";
						updateObj = new Query(datasource = application.dnsMaster);
						
						updateObj.addParam(name="taskId", value=taskId, cfsqltype="cf_sql_varchar");
						updateObj.addParam(name="markDone", value=rs_mark_prog.mark_done, cfsqltype="cf_sql_integer");
						updateObj.addParam(name="markNeed", value=rs_mark_prog.mark_need, cfsqltype="cf_sql_integer");
						
						updateObj.execute(sql=sql);
						
						progress = 0;
					
						sql = "	SELECT
									ttm.task_id,
									ttm.task_prog,
									ttm.mark_need,
									ttm.mark_done
								FROM
									t_task_mark ttm
								WHERE
									ttm.task_id = :taskId ";
							
						queryObj = new Query(datasource = application.dnsMaster);
						
						queryObj.addParam(name="taskId", value=taskId, cfsqltype="cf_sql_varchar");
						
						rs_task = queryObj.execute(sql=sql).getResult();
					
						if ( rs_task.mark_need ) {
							progress = int( rs_task.mark_done * 10000 / rs_task.mark_need ) / 100;
						}
					
						structInsert(response, "success", true, true);
						structInsert( response, "MarkNeed", rs_task.mark_need );
						structInsert( response, "MarkDone", rs_task.mark_done );
						structInsert( response, "Progress", progress );
					
					}
					
				}
				else {
					structInsert(response, "reason", "验证数据失效", true);
				}
				
			}
			else {
				structInsert(response, "reason", "验证数据失效", true);
			}
			
		}
		catch (any ecpt) {
				structInsert(response, "reason", "验证数据失效", true);
		}
		
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>